package com.thinkfit.jy.fragment;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.common.lib.bean.VersionBean;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.manager.DataManager;
import com.thinkfit.jy.R;
import com.thinkfit.jy.activity.MangeModeActivity;
import com.thinkfit.jy.activity.SettingActivity;
import com.thinkfit.jy.bluetooth.Bluetooth;
import com.thinkfit.jy.contract.SettingContract;
import com.thinkfit.jy.presenter.SettingPresenter;
import com.thinkfit.jy.utils.BluetoothOrder;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.MotionDataUtils;

/**
 * 设置菜单
 */
public class SettingFragment extends BaseFragment<SettingContract.Presenter> implements SettingContract.View {
    @NonNull
    @Override
    protected SettingContract.Presenter onCreatePresenter() {
        return new SettingPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return DataManager.Companion.getInstance().getMainStyle() == 3 ? R.layout.fragment_setting_xm : R.layout.fragment_setting;
    }

    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        setViewsOnClickListener(R.id.llWifi, R.id.llBluetooth, R.id.llSwitchLanguage, R.id.llSwitchDevice,
                R.id.llRegion, R.id.llUnits, R.id.tvAbout, R.id.tvManageMode, R.id.tvScreenProtect);//, R.id.llUpdate);
        showUnit(DataManager.Companion.getInstance().getUnit());
    }

    public void onResume() {
        super.onResume();
        setText(R.id.tvLanguage,
                DataManager.Companion.getInstance().getLanguage() == 0 ? R.string.app_simple_chinese : R.string.app_english);
        int region = DataManager.Companion.getInstance().getRegion();
        setText(R.id.tvRegion,
                region == 0 ? R.string.app_asia :
                        (region == 1 ? R.string.app_america : R.string.app_europe));

        WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();

        String ssid = wifiInfo.getSSID().replace("\"", "");
        if (ssid.equals("<unknown ssid>")) {
            setText(R.id.tvWifi, R.string.app_not_connect);
        } else {
            setText(R.id.tvWifi, ssid);
        }
        setText(R.id.tvBluetooth, Bluetooth.getInstance().isBluetoothOpened() ? R.string.app_open : R.string.app_close);
        //setText(R.id.tvDevice);
        if(DeviceTypeUtils.isConnect()){
            if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.SPINNING)){
                setText(R.id.tvDevice,getString(R.string.app_device_type_1));
            }else if(MotionDataUtils.mCurrentDeviceType.equals(DeviceTypeUtils.TREADMILL)){
                setText(R.id.tvDevice,getString(R.string.app_device_type_0));
            }
            setText(R.id.tvDevice, StringUtils.isEmpty(BluetoothOrder.getInstance().getBluetoothName()) ?
                    "":BluetoothOrder.getInstance().getBluetoothName());
        }else {
            setText(R.id.tvDevice,"");
        }
    }

    private void showUnit(boolean isUnit) {
        setImage(R.id.ivUnitSwitch, isUnit ? R.drawable.app_switch_on :
                (DataManager.Companion.getInstance().getMainStyle() == 3 ? R.drawable.app_switch_off_xm : R.drawable.app_switch_off));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.llWifi://wifi
                ((SettingActivity) getActivity()).toFragment(new WifiConnectFragment());
                break;
            case R.id.llBluetooth://蓝牙
                ((SettingActivity) getActivity()).toFragment(new BluetoothFragment());
                break;
            case R.id.llSwitchLanguage://切换语言
                ((SettingActivity) getActivity()).toFragment(new SwitchLanguageFragment());
                break;
            case R.id.llRegion://切换地区
                ((SettingActivity) getActivity()).toFragment(new SwitchRegionFragment());
                break;
            case R.id.llUnits://使用英制单位开关
                if (DataManager.Companion.getInstance().getUnit()) {
                    DataManager.Companion.getInstance().saveUnit(false);
                    setImage(R.id.ivUnitSwitch, (DataManager.Companion.getInstance().getMainStyle() == 3 ? R.drawable.app_switch_off_xm : R.drawable.app_switch_off));
                } else {
                    DataManager.Companion.getInstance().saveUnit(true);
                    setImage(R.id.ivUnitSwitch, R.drawable.app_switch_on);
                }
                break;
            case R.id.tvAbout://关于
                ((SettingActivity) getActivity()).toFragment(new AboutFragment());
                break;
            case R.id.tvManageMode://管理模式
                openActivity(MangeModeActivity.class);
                break;
            case R.id.tvScreenProtect://屏保
                ((SettingActivity) getActivity()).toFragment(new ScreenProtectFragment());
                break;
            case R.id.llSwitchDevice://切换设备
                if(!DeviceTypeUtils.isConnect()){
                    ToastUtils.showShort("读取连接的设备");
                }

                break;
//            case R.id.llUpdate:
//                getPresenter().getVersion();
//                break;
        }
    }

    @Override
    public void getVersionSuccess(VersionBean versionBean) {
        if (getView() == null) {
            return;
        }
        showToast(R.string.app_current_is_newest);
    }
}
